package f.f0.h;

import c.j.a.c.y.a.i;
import f.c0;
import f.r;
import f.v;
import g.a0;
import g.l;
import g.q;
import g.u;
import g.y;
import g.z;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Proxy.Type;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/* compiled from: Http1Codec */
public final class a implements f.f0.g.c {
    public final v a;
    public final f.f0.f.g b;
    public final g.g c;
    public final g.f d;
    public int e = 0;
    public long f = 262144;

    /* compiled from: Http1Codec */
    public abstract class b implements z {
        public final l a;
        public boolean b;
        public long c = 0;

        public b(a aVar) {
            this.a = new l(a.this.c.B());
        }

        public a0 B() {
            return this.a;
        }

        public final void a(boolean z, IOException iOException) throws IOException {
            a aVar = a.this;
            int i = aVar.e;
            if (i != 6) {
                if (i == 5) {
                    aVar.g(this.a);
                    a aVar2 = a.this;
                    aVar2.e = 6;
                    f.f0.f.g gVar = aVar2.b;
                    if (gVar != null) {
                        gVar.i(z ^ 1, aVar2, this.c, iOException);
                    }
                    return;
                }
                StringBuilder g = c.b.a.a.a.g("state: ");
                g.append(a.this.e);
                throw new IllegalStateException(g.toString());
            }
        }

        public long c(g.e eVar, long j) throws IOException {
            try {
                long c = a.this.c.c(eVar, j);
                if (c > 0) {
                    this.c += c;
                }
                return c;
            } catch (IOException e) {
                a(false, e);
                throw e;
            }
        }
    }

    /* compiled from: Http1Codec */
    public final class c implements y {
        public final l a;
        public boolean b;

        public c() {
            this.a = new l(a.this.d.B());
        }

        public a0 B() {
            return this.a;
        }

        public void E(g.e eVar, long j) throws IOException {
            if (this.b) {
                throw new IllegalStateException("closed");
            } else if (j != 0) {
                a.this.d.H(j);
                String str = "\r\n";
                a.this.d.j0(str);
                a.this.d.E(eVar, j);
                a.this.d.j0(str);
            }
        }

        public synchronized void close() throws IOException {
            if (!this.b) {
                this.b = true;
                a.this.d.j0("0\r\n\r\n");
                a.this.g(this.a);
                a.this.e = 3;
            }
        }

        public synchronized void flush() throws IOException {
            if (!this.b) {
                a.this.d.flush();
            }
        }
    }

    /* compiled from: Http1Codec */
    public final class e implements y {
        public final l a;
        public boolean b;
        public long c;

        public e(long j) {
            this.a = new l(a.this.d.B());
            this.c = j;
        }

        public a0 B() {
            return this.a;
        }

        public void E(g.e eVar, long j) throws IOException {
            if (this.b) {
                throw new IllegalStateException("closed");
            }
            f.f0.c.e(eVar.b, 0, j);
            if (j <= this.c) {
                a.this.d.E(eVar, j);
                this.c -= j;
                return;
            }
            StringBuilder g = c.b.a.a.a.g("expected ");
            g.append(this.c);
            g.append(" bytes but received ");
            g.append(j);
            throw new ProtocolException(g.toString());
        }

        public void close() throws IOException {
            if (!this.b) {
                this.b = true;
                if (this.c <= 0) {
                    a.this.g(this.a);
                    a.this.e = 3;
                    return;
                }
                throw new ProtocolException("unexpected end of stream");
            }
        }

        public void flush() throws IOException {
            if (!this.b) {
                a.this.d.flush();
            }
        }
    }

    /* compiled from: Http1Codec */
    public class d extends b {
        public final r e;
        public long f = -1;
        public boolean g = true;

        public d(r rVar) {
            super(null);
            this.e = rVar;
        }

        public long c(g.e eVar, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
            } else if (this.b) {
                throw new IllegalStateException("closed");
            } else if (!this.g) {
                return -1;
            } else {
                long j2 = this.f;
                if (j2 == 0 || j2 == -1) {
                    if (j2 != -1) {
                        a.this.c.M();
                    }
                    try {
                        this.f = a.this.c.n0();
                        String trim = a.this.c.M().trim();
                        if (this.f < 0 || !(trim.isEmpty() || trim.startsWith(";"))) {
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append("expected chunk size and optional extensions but was \"");
                            stringBuilder.append(this.f);
                            stringBuilder.append(trim);
                            stringBuilder.append("\"");
                            throw new ProtocolException(stringBuilder.toString());
                        }
                        if (this.f == 0) {
                            this.g = false;
                            a aVar = a.this;
                            f.f0.g.e.d(aVar.a.i, this.e, aVar.j());
                            a(true, null);
                        }
                        if (!this.g) {
                            return -1;
                        }
                    } catch (NumberFormatException e) {
                        throw new ProtocolException(e.getMessage());
                    }
                }
                long c = super.c(eVar, Math.min(j, this.f));
                if (c != -1) {
                    this.f -= c;
                    return c;
                }
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                a(false, protocolException);
                throw protocolException;
            }
        }

        public void close() throws IOException {
            if (!this.b) {
                if (this.g && !f.f0.c.l(this, 100, TimeUnit.MILLISECONDS)) {
                    a(false, null);
                }
                this.b = true;
            }
        }
    }

    /* compiled from: Http1Codec */
    public class f extends b {
        public long e;

        public f(a aVar, long j) throws IOException {
            super(null);
            this.e = j;
            if (j == 0) {
                a(true, null);
            }
        }

        public long c(g.e eVar, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
            } else if (this.b) {
                throw new IllegalStateException("closed");
            } else {
                long j2 = this.e;
                if (j2 == 0) {
                    return -1;
                }
                long c = super.c(eVar, Math.min(j2, j));
                if (c != -1) {
                    j2 = this.e - c;
                    this.e = j2;
                    if (j2 == 0) {
                        a(true, null);
                    }
                    return c;
                }
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                a(false, protocolException);
                throw protocolException;
            }
        }

        public void close() throws IOException {
            if (!this.b) {
                if (!(this.e == 0 || f.f0.c.l(this, 100, TimeUnit.MILLISECONDS))) {
                    a(false, null);
                }
                this.b = true;
            }
        }
    }

    /* compiled from: Http1Codec */
    public class g extends b {
        public boolean e;

        public g(a aVar) {
            super(null);
        }

        public long c(g.e eVar, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
            } else if (this.b) {
                throw new IllegalStateException("closed");
            } else if (this.e) {
                return -1;
            } else {
                long c = super.c(eVar, j);
                if (c != -1) {
                    return c;
                }
                this.e = true;
                a(true, null);
                return -1;
            }
        }

        public void close() throws IOException {
            if (!this.b) {
                if (!this.e) {
                    a(false, null);
                }
                this.b = true;
            }
        }
    }

    public a(v vVar, f.f0.f.g gVar, g.g gVar2, g.f fVar) {
        this.a = vVar;
        this.b = gVar;
        this.c = gVar2;
        this.d = fVar;
    }

    public void a() throws IOException {
        this.d.flush();
    }

    public void b(f.y yVar) throws IOException {
        Type type = this.b.b().c.b.type();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(yVar.b);
        stringBuilder.append(' ');
        Object obj = (yVar.a.a.equals("https") || type != Type.HTTP) ? null : 1;
        if (obj != null) {
            stringBuilder.append(yVar.a);
        } else {
            stringBuilder.append(i.F(yVar.a));
        }
        stringBuilder.append(" HTTP/1.1");
        k(yVar.c, stringBuilder.toString());
    }

    public c0 c(f.a0 a0Var) throws IOException {
        Objects.requireNonNull(this.b.f);
        String c = a0Var.f.c("Content-Type");
        String str = null;
        if (c == null) {
            c = null;
        }
        z h;
        if (f.f0.g.e.b(a0Var)) {
            String c2 = a0Var.f.c("Transfer-Encoding");
            if (c2 != null) {
                str = c2;
            }
            String str2 = "state: ";
            Logger logger;
            StringBuilder g;
            if ("chunked".equalsIgnoreCase(str)) {
                r rVar = a0Var.a.a;
                if (this.e == 4) {
                    this.e = 5;
                    d dVar = new d(rVar);
                    logger = q.a;
                    return new f.f0.g.g(c, -1, new u(dVar));
                }
                g = c.b.a.a.a.g(str2);
                g.append(this.e);
                throw new IllegalStateException(g.toString());
            }
            long a = f.f0.g.e.a(a0Var);
            if (a != -1) {
                h = h(a);
                logger = q.a;
                return new f.f0.g.g(c, a, new u(h));
            } else if (this.e == 4) {
                f.f0.f.g gVar = this.b;
                if (gVar != null) {
                    this.e = 5;
                    gVar.f();
                    g gVar2 = new g(this);
                    logger = q.a;
                    return new f.f0.g.g(c, -1, new u(gVar2));
                }
                throw new IllegalStateException("streamAllocation == null");
            } else {
                g = c.b.a.a.a.g(str2);
                g.append(this.e);
                throw new IllegalStateException(g.toString());
            }
        }
        h = h(0);
        Logger logger2 = q.a;
        return new f.f0.g.g(c, 0, new u(h));
    }

    public void cancel() {
        f.f0.f.c b = this.b.b();
        if (b != null) {
            f.f0.c.g(b.d);
        }
    }

    public void d() throws IOException {
        this.d.flush();
    }

    public y e(f.y yVar, long j) {
        String str = "state: ";
        StringBuilder g;
        if ("chunked".equalsIgnoreCase(yVar.c.c("Transfer-Encoding"))) {
            if (this.e == 1) {
                this.e = 2;
                return new c();
            }
            g = c.b.a.a.a.g(str);
            g.append(this.e);
            throw new IllegalStateException(g.toString());
        } else if (j == -1) {
            throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
        } else if (this.e == 1) {
            this.e = 2;
            return new e(j);
        } else {
            g = c.b.a.a.a.g(str);
            g.append(this.e);
            throw new IllegalStateException(g.toString());
        }
    }

    public f.a0.a f(boolean z) throws IOException {
        int i = this.e;
        if (i == 1 || i == 3) {
            try {
                f.f0.g.i a = f.f0.g.i.a(i());
                f.a0.a aVar = new f.a0.a();
                aVar.b = a.a;
                aVar.c = a.b;
                aVar.d = a.c;
                aVar.e(j());
                if (z && a.b == 100) {
                    return null;
                }
                if (a.b == 100) {
                    this.e = 3;
                    return aVar;
                }
                this.e = 4;
                return aVar;
            } catch (EOFException e) {
                StringBuilder g = c.b.a.a.a.g("unexpected end of stream on ");
                g.append(this.b);
                IOException iOException = new IOException(g.toString());
                iOException.initCause(e);
                throw iOException;
            }
        }
        StringBuilder g2 = c.b.a.a.a.g("state: ");
        g2.append(this.e);
        throw new IllegalStateException(g2.toString());
    }

    public void g(l lVar) {
        a0 a0Var = lVar.e;
        lVar.e = a0.d;
        a0Var.a();
        a0Var.b();
    }

    public z h(long j) throws IOException {
        if (this.e == 4) {
            this.e = 5;
            return new f(this, j);
        }
        StringBuilder g = c.b.a.a.a.g("state: ");
        g.append(this.e);
        throw new IllegalStateException(g.toString());
    }

    public final String i() throws IOException {
        String b0 = this.c.b0(this.f);
        this.f -= (long) b0.length();
        return b0;
    }

    public f.q j() throws IOException {
        f.q.a aVar = new f.q.a();
        while (true) {
            String i = i();
            if (i.length() == 0) {
                return new f.q(aVar);
            }
            Objects.requireNonNull((f.v.a) f.f0.a.a);
            String str = ":";
            int indexOf = i.indexOf(str, 1);
            if (indexOf != -1) {
                aVar.a(i.substring(0, indexOf), i.substring(indexOf + 1));
            } else {
                String str2 = "";
                if (i.startsWith(str)) {
                    i = i.substring(1);
                    aVar.a.add(str2);
                    aVar.a.add(i.trim());
                } else {
                    aVar.a.add(str2);
                    aVar.a.add(i.trim());
                }
            }
        }
    }

    public void k(f.q qVar, String str) throws IOException {
        if (this.e == 0) {
            String str2 = "\r\n";
            this.d.j0(str).j0(str2);
            int f = qVar.f();
            for (int i = 0; i < f; i++) {
                this.d.j0(qVar.d(i)).j0(": ").j0(qVar.g(i)).j0(str2);
            }
            this.d.j0(str2);
            this.e = 1;
            return;
        }
        StringBuilder g = c.b.a.a.a.g("state: ");
        g.append(this.e);
        throw new IllegalStateException(g.toString());
    }
}
